<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    sitepageevent
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: WidgetSettings.php 6590 2010-05-05 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */

//GET DB
$db = Zend_Db_Table_Abstract::getDefaultAdapter ();

//CHECK THAT SITEPAGE PLUGIN IS ACTIVATED OR NOT
$select = new Zend_Db_Select ( $db );
$select->from ( 'engine4_core_settings' )->where ( 'name = ?', 'sitepage.is.active' )->limit ( 1 );
$sitepage_settings = $select->query ()->fetchAll ();
if (! empty ( $sitepage_settings )) {
	$sitepage_is_active = $sitepage_settings [0] ['value'];
} else {
	$sitepage_is_active = 0;
}

//CHECK THAT SITEPAGE PLUGIN IS INSTALLED OR NOT
$select = new Zend_Db_Select ( $db );
$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepage' )->where ( 'enabled = ?', 1 );
$check_sitepage = $select->query ()->fetchObject ();
if (! empty ( $check_sitepage ) && ! empty ( $sitepage_is_active )) {
	$select = new Zend_Db_Select ( $db );
	$select_page = $select->from ( 'engine4_core_pages', 'page_id' )->where ( 'name = ?', 'sitepage_index_view' )->limit ( 1 );
	$page = $select_page->query ()->fetchAll ();
	if (! empty ( $page )) {
		$page_id = $page [0] ['page_id'];
		
		//INSERT THE BADGE WIDGET IN CORE CONTENT TABLE
		$select = new Zend_Db_Select ( $db );
		$select_content = $select->from ( 'engine4_core_content' )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'widget' )->where ( 'name = ?', 'sitepagebadge.badge-sitepagebadge' )->limit ( 1 );
		$content = $select_content->query ()->fetchAll ();
		if (empty ( $content )) {
			$select = new Zend_Db_Select ( $db );
			$select_container = $select->from ( 'engine4_core_content', 'content_id' )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'container' )->limit ( 1 );
			$container = $select_container->query ()->fetchAll ();
			if (! empty ( $container )) {
				$container_id = $container [0] ['content_id'];
				$select = new Zend_Db_Select ( $db );
				$select_left = $select->from ( 'engine4_core_content' )->where ( 'parent_content_id = ?', $container_id )->where ( 'type = ?', 'container' )->where ( 'name = ?', 'left' )->limit ( 1 );
				$left = $select_left->query ()->fetchAll ();
				if (! empty ( $left )) {
					$left_id = $left [0] ['content_id'];
					$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'sitepagebadge.badge-sitepagebadge', 'parent_content_id' => $left_id, 'order' => 16, 'params' => '{"title":"Badge","titleCount":""}' ) );
				}
			}
		}
		
		//INSERT THE BADGE WIDGET IN CORE CONTENT TABLE
		$select = new Zend_Db_Select ( $db );
		$select_content = $select->from ( 'engine4_sitepage_admincontent' )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'widget' )->where ( 'name = ?', 'sitepagebadge.badge-sitepagebadge' )->limit ( 1 );
		$content = $select_content->query ()->fetchAll ();
		if (empty ( $content )) {
			$select = new Zend_Db_Select ( $db );
			$select_container = $select->from ( 'engine4_sitepage_admincontent', 'admincontent_id' )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'container' )->limit ( 1 );
			$container = $select_container->query ()->fetchAll ();
			if (! empty ( $container )) {
				$container_id = $container [0] ['admincontent_id'];
				$select = new Zend_Db_Select ( $db );
				$select_left = $select->from ( 'engine4_sitepage_admincontent' )->where ( 'parent_content_id = ?', $container_id )->where ( 'type = ?', 'container' )->where ( 'name = ?', 'left' )->limit ( 1 );
				$left = $select_left->query ()->fetchAll ();
				if (! empty ( $left )) {
					$left_id = $left [0] ['admincontent_id'];
					$db->insert ( 'engine4_sitepage_admincontent', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'sitepagebadge.badge-sitepagebadge', 'parent_content_id' => $left_id, 'order' => 16, 'params' => '{"title":"Badge","titleCount":""}' ) );
				}
			}
		}
		
		//INSERTING THE FORM WIDGET IN SITEPAGE_CONTENT TABLE ALSO.
		$select = new Zend_Db_Select ( $db );
		$contentpage_ids = $select->from ( 'engine4_sitepage_contentpages', 'contentpage_id' )->query ()->fetchAll ();
		
		foreach ( $contentpage_ids as $contentpage_id ) {
			if (! empty ( $contentpage_id )) {
				$select = new Zend_Db_Select ( $db );
				$select_content = $select->from ( 'engine4_sitepage_content' )->where ( 'contentpage_id = ?', $contentpage_id ['contentpage_id'] )->where ( 'type = ?', 'widget' )->where ( 'name = ?', 'sitepagebadge.badge-sitepagebadge' )->limit ( 1 );
				$content = $select_content->query ()->fetchAll ();
				if (empty ( $content )) {
					$select = new Zend_Db_Select ( $db );
					$select_container = $select->from ( 'engine4_sitepage_content', 'content_id' )->where ( 'contentpage_id = ?', $contentpage_id ['contentpage_id'] )->where ( 'type = ?', 'container' )->limit ( 1 );
					$container = $select_container->query ()->fetchAll ();
					if (! empty ( $container )) {
						$container_id = $container [0] ['content_id'];
						$select = new Zend_Db_Select ( $db );
						$select_left = $select->from ( 'engine4_sitepage_content' )->where ( 'parent_content_id = ?', $container_id )->where ( 'type = ?', 'container' )->where ( 'name = ?', 'left' )->limit ( 1 );
						$left = $select_left->query ()->fetchAll ();
						if (! empty ( $left )) {
							$left_id = $left [0] ['content_id'];
							$db->insert ( 'engine4_sitepage_content', array ('contentpage_id' => $contentpage_id ['contentpage_id'], 'type' => 'widget', 'name' => 'sitepagebadge.badge-sitepagebadge', 'parent_content_id' => $left_id, 'order' => 16, 'params' => '{"title":"Badge","titleCount":""}' ) );
						}
					}
				}
			}
		}
	}
}

?>